using System.Windows;
using System.Windows.Controls;

namespace OpenLS.Spreadsheet.UI.Internals
{
    public class ReverseZIndexStackPanel : VirtualizingStackPanel
    {


        public static bool  GetZTop (DependencyObject obj)
        {
            return (bool)obj.GetValue(ZTopProperty);
        }

        public static void SetZTop(DependencyObject obj, bool value)
        {
            obj.SetValue(ZTopProperty, value);
        }

        // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ZTopProperty =
            DependencyProperty.RegisterAttached("ZTop", typeof(bool), typeof(ReverseZIndexStackPanel), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsParentArrange ));

        protected override Size ArrangeOverride(Size arrangeSize)
        {
            int zIndex = Children.Count;
            foreach (UIElement child in Children)
            {
                if (GetZTop(child))
                    Panel.SetZIndex(child, Children.Count + 1);
                else
                {
                    Panel.SetZIndex(child, zIndex);
                }
                zIndex--;
            }
            return base.ArrangeOverride(arrangeSize);
        }
        
    }
}